<?php
namespace Admin\Controller;

class RoleController extends PublicController
{
    public function index()
    {
        $list = M('role')->select();
        $this->assign('list', $list);
        $this->display('Role/index');
    }

    //执行权限启用或禁用
    public function status()
    {
        //判断GET里有无id
        if (empty($_GET['id'])) {
            $this->redirect('Role/index');
        }

        //过滤非法id值
        $id = I('get.id/d');
        $status = I('get.status/d');
        $status = ($status=='1')?'0':'1';
        if ($id <= 0) {
            $this->error('禁止传入非法数字或负数', U('Role/index'));
        } else {
            M('role')->where(array('id'=>$id))->save(array('status'=>$status));
            $this->redirect('Role/index');
        }
    }

    //添加权限显示
    public function addRole()
    {
        $this->display('Role/add');
    }

    //执行添加
    public function add()
    {
        if (IS_POST) {
            if (!D('Role')->create()) {
                $this->error(D('Role')->getError());
            } else {
                if (D('Role')->add()) {
                    $this->success('添加成功',U('Role/index'));
                } else {
                    $this->error('添加失败');
                }
            }
        } else {
            $this->error('禁止非法添加');
        }
    }

    //编辑权限
    public function edit()
    {
        //判断GET里有无id
        if (empty($_GET['id'])) {
            $this->redirect('Role/index');
        }

        //过滤非法id值
        $id = I('get.id/d');
        $list = M('role')->where('id='.$id)->select();
        $this->assign('id', $id);
        $this->assign('list', $list);
        $this->display('Role/edit');
    }

    //执行编辑
    public function update()
    {
        $id = I('post.id/d');
        if ($id <= 0) {
            $this->error('id不能传入非法数字或负数');
        } else {
            if (!IS_POST) {
                $this->error('禁止非法传入数据');
            } else {
                $role = D('role');
                if (!$role->create()) {
                    // v($_POST);exit;
                    $this->error($role->getError());
                } else {
                    if ($role->where('id='.$id)->save()) {
                        $this->success('编辑成功',U('Role/index'));
                    } else {
                        $this->error('编辑失败');
                    }
                }
            }
        }
    }

    //执行删除
    public function del()
    {
        //判断GET中有无id值
        if (empty($_GET['id'])) {
            $this->redirect('Role/index');
        }

        $id = I('get.id/d');
        if ($id <= 0) {
            $this->error('id不能传入非法数字或负数');
        } else {
            if (M('role')->where('id='.$id)->delete($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
    }


}
